package com.hoodbrains.insideloc.maps;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hoodbrains.insideloc.R;
import com.hoodbrains.insideloc.shopping.RayonChoiceActivity;
import com.hoodbrains.insideloc.shopping.ShoppingItem;
import com.hoodbrains.insideloc.utils.Catalog;
import com.hoodbrains.insideloc.utils.ItemHolder;

public class EditGondoleActivity extends Activity 
	implements OnClickListener {
	
	Gondole gondole;
	LinearLayout viewCote1;
	LinearLayout viewCote2;
	List<Rayon> cote1, cote2;
	
	private static final int SPACER = 1;
	private static final int RAYON = 2;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.edit_gondole);
		viewCote1 = (LinearLayout)findViewById(R.id.cote1);
		viewCote2 = (LinearLayout)findViewById(R.id.cote2);
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		gondole = Catalog.getInstance().getCurrentCondole();
		
		cote1 = gondole.getCote1();
		cote2 = gondole.getCote2();
		
		resetView();
	}
	
	private TextView baseView(int id, ItemHolder<Rayon>iH, int height) {
		TextView tv = new TextView(this);
		tv.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		tv.setHeight(height);
		tv.setId(id);
		tv.setTag(iH);
		tv.setOnClickListener(this);
		return tv;
	}
	
	private TextView rayonView(Rayon rayon, int position, int height) {
		TextView view = baseView(RAYON, new ItemHolder<Rayon>(rayon, position), height);
		view.setText("x");
		view.setBackgroundColor(Color.parseColor(rayon.getColor()));
		return view;
		
	}
	
	private TextView spacerView(Rayon rayon, int position, int height) {
		TextView view = baseView(SPACER, new ItemHolder<Rayon>(rayon, position), height);
		view.setText("+");
		return view;
	}
	
	private void resetView() {
		
		viewCote1.removeAllViews();
		int height1 = 480 / (2*cote1.size() + 1);
		viewCote1.addView(spacerView(null, 1, height1));
		for (int i=0; i<cote1.size(); i++) {
			viewCote1.addView(rayonView(cote1.get(i), i, height1));
			viewCote1.addView(spacerView(cote1.get(i), i+2, height1));
		}
		viewCote2.removeAllViews();
		int height2 = 480 / (2*cote2.size() + 1);
		viewCote2.addView(spacerView(null, -1, height2));
		for (int i=0; i<cote2.size(); i++) {
			viewCote2.addView(rayonView(cote2.get(i), -i, height2));
			viewCote2.addView(spacerView(cote2.get(i), -(i+2), height2));
		}
		
	}

	public void onClick(View v) {
		switch(v.getId()) {
		case SPACER : {
//			//Create the place.
			
			Intent intent = new Intent(this, RayonChoiceActivity.class);
			intent.putExtra("rayon_position",ItemHolder.<Rayon>getItemHolder(v).getPosition());
			startActivityForResult(intent, 74);
			
			break;
		}
			
		case RAYON : {
//			if (ItemHolder.<Rayon>getItemHolder(v).getPosition() > 0) {
			cote1.remove(ItemHolder.<Rayon>getItem(v));
//			} else {
			cote2.remove(ItemHolder.<Rayon>getItem(v));
//				
//			}
			break;
		}
		}
	
		resetView();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == 74) {
			int position = data.getIntExtra("rayon_position", 0);
			if (position > 0 ) {
				cote1.add(position-1, new Rayon(data.getStringExtra("rayon")));
			} else {
				cote2.add(-position-1, new Rayon(data.getStringExtra("rayon")));
				
			}
		resetView();
		}
		
	}
	
	
		
}
